<?php defined('BASEPATH') or exit('No direct script access allowed');
class L_user_m extends L_Model
{
	public function __construct(){	
		parent::__construct();
		$this->load->model('report/report_m');
	}
	
	public function add_l_user($user_id,
				        $user_type,
				        $commission_rate,
				        $affiliate_id,
				        $affiliate_rate,
				        $affiliate_commission_rate,
				        $agent_id,
				        $agent_rate, 
				        $lr_acc) {
		$default_dbprefix = $this->db->dbprefix;
		$this->db->dbprefix='l_';
		
		$result = $this->db->insert('user', array(
			'user_id'					=> $user_id,
			'user_type'					=> $user_type,
			'commission_rate'			=> $commission_rate,
			'affiliate_id'				=> $affiliate_id,
			'affiliate_rate'			=> $affiliate_rate,
			'affiliate_commission_rate' => $affiliate_commission_rate,
			'agent_id'					=> $agent_id,
			'agent_rate'				=> $agent_rate,
			'lr_acc'					=> $lr_acc
		
		));
		$this->db->dbprefix = $default_dbprefix;
		return $this->db->insert_id();
	}
	
	/**
	 * return value of this method is used to set to USERDATA session.
	 * 
	 * @param unknown_type $user_id
	 */
	public function get_l_user_data($user_id) {
		$sql = "SELECT  id as l_user_id, 
					    user_type,
				  		commission_rate,
				  		affiliate_id,
				 		affiliate_rate,
				  		affiliate_commission_rate,
				  		agent_id,
				  		agent_rate,
				  		lr_acc  
				FROM l_user 
				WHERE user_id = ?";
		return $this->db->query($sql, array($user_id))->row_array();
	}
	
	public function get_account_balance($user_id) {
		$acc_balance = $this->report_m->getMemberBalance(NULL, NULL, $user_id);
		
		$balance_amount = 0;
		if (isset($acc_balance) && !empty($acc_balance)) {
			$balance_amount = sizeof($acc_balance) >= 0 ? $acc_balance[0]->balance : 0;
		}
		return $balance_amount;
	}
	
	public function update_user_lr_acc($user_id, $new_lr_acc) {
		$default_dbprefix = $this->db->dbprefix;
		$this->db->dbprefix='l_';
		
		$result = $this->db->update('user', array('lr_acc' => $new_lr_acc), array('user_id' => $user_id));
		$this->db->dbprefix = $default_dbprefix;
	}
}